package com.xjrsoft.module.customer.erp_model.goodsManage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.customer.erp_model.goodsManage.dto.ErpGoodsDto;
import com.xjrsoft.module.customer.erp_model.goodsManage.dto.SaveErpGoodsFormDataDto;
import com.xjrsoft.module.customer.erp_model.goodsManage.vo.ErpGoodsVo;
import com.xjrsoft.module.customer.erp_model.goodsManage.vo.ErpGoodsListVo;
import com.xjrsoft.module.customer.erp_model.goodsManage.dto.ErpGoodsListDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import com.xjrsoft.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customer.erp_model.goodsManage.entity.ErpGoods;
import com.xjrsoft.module.customer.erp_model.goodsManage.vo.ErpGoodsVo;
import com.xjrsoft.module.customer.erp_model.goodsManage.service.IErpGoodsService;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-10-09
 */
@RestController
@AllArgsConstructor
@RequestMapping("/goodsmanage")
@Api(value = "商品管理", tags = "商品管理接口")
public class ErpGoodsController {


	private final IErpGoodsService erpGoodsService;


	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpGoods")
	public Response<ErpGoodsVo> getFormData(@PathVariable String id) {
		// 主表数据
		ErpGoods erpGoods = erpGoodsService.getById(id);
		return Response.ok(BeanUtil.copy(erpGoods, ErpGoodsVo.class));
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpGoods")
	public Response<PageOutput<ErpGoodsListVo>> getPageList(ErpGoodsListDto listDto) {
		IPage<ErpGoods> page = erpGoodsService.getPageList(listDto);
		List<ErpGoodsListVo> records = BeanUtil.copyList(page.getRecords(), ErpGoodsListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		// 工作流数据处理
		//FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入erpGoods")
	public Response save(@RequestBody SaveErpGoodsFormDataDto formDto) {
		ErpGoodsDto erpGoodsDto = formDto.getErpGoodsDto();
		ErpGoods erpGoods = BeanUtil.copy(erpGoodsDto, ErpGoods.class);
		String dataId = erpGoodsService.addErpGoods(erpGoods);
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入erpGoods")
	public Response update(@PathVariable String id, @RequestBody SaveErpGoodsFormDataDto formDto) {
		ErpGoodsDto erpGoodsDto = formDto.getErpGoodsDto();
		ErpGoods erpGoods = BeanUtil.copy(erpGoodsDto, ErpGoods.class);
		return Response.status(erpGoodsService.updateErpGoods(id, erpGoods));
	}


	
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpGoodsService.removeByIds(Func.toStrList(ids)));
	}

	
}
